package com.itheima.area.controller;


import com.github.pagehelper.Page;
import com.itheima.area.pojo.CustFarm;
import com.itheima.area.service.FarmService;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.wiring.ClassNameBeanWiringInfoResolver;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * @PackageName com.itheima.area.controller
 * @ProjectName Ceres-Work
 * @Created by Chanly on 2020/10/11 20:13
 **/

@RequestMapping("/farm")
@RestController
public class FarmController {
    @Autowired
    private FarmService farmService ;

    /**
     * 根据ID查询
     * @return
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Long id){
        try {
            CustFarm custFarm = farmService.findById(id);
            return new Result(true, StatusCode.OK,"查询成功",custFarm);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"查询失败");
        }
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        try {
            List<CustFarm> custFarmList = farmService.findAll();
            return new Result(true,StatusCode.OK,"查询所有成功",custFarmList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"查询所有失败");
        }
    }
    /**
     * 删除
     * @param id
     * @return
     */
   @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id){
       try {
           farmService.deleteById(id);
           return new Result(true,StatusCode.OK,"删除成功");
       } catch (Exception e) {
           e.printStackTrace();
           return new Result(false,StatusCode.ERROR,"删除成功");
       }
   }

    /**
     * 更新农田
     * @param custFarm
     * @param
     * @return
     */
   @PutMapping("/updateFarm")
    public Result update( @RequestBody CustFarm custFarm ){
       try {
           farmService.update(custFarm);
           return new Result(true,StatusCode.OK,"更新成功");
       } catch (Exception e) {
           e.printStackTrace();
           return new Result(false,StatusCode.ERROR,"更新失败");
       }
   }

    /**
     * 添加
     * @param custFarm
     * @return
     */
   @PostMapping("/add")
    public Result add(@RequestBody CustFarm custFarm){
       try {
           farmService.add(custFarm);
           return new Result(true,StatusCode.OK,"添加成功");
       } catch (Exception e) {
           e.printStackTrace();
           return new Result(false,StatusCode.ERROR,"添加农田失败");
       }
   }

    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestParam Map searchMap, @PathVariable  int page, @PathVariable  int size){
        try {
            Page<CustFarm> pageList = farmService.findPage(searchMap, page, size);
            PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
            return new Result(true,StatusCode.OK,"查询成功",pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"分页查询失败");
        }
    }
}
